package com.ruoyi.web.controller.system.carPack;

import java.util.List;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.carPark.FzCarPark;
import com.ruoyi.system.domain.carPark.FzCarlot;
import com.ruoyi.system.domain.communiry.FzSCommunity;
import com.ruoyi.system.service.carPark.IFzCarParkService;
import com.ruoyi.system.service.carPark.IFzCarlotService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 车位信息Controller
 * 
 * @author ruoyi
 * @date 2020-03-03
 */
@Controller
@RequestMapping("/system/carlot")
public class FzCarlotController extends BaseController
{
    private String prefix = "system/carPark/carlot";
    @Autowired
    private IFzCarParkService fzCarParkService;
    @Autowired
    private IFzCarlotService fzCarlotService;

    @RequiresPermissions("system:carlot:view")
    @GetMapping()
    public String carlot(ModelMap map,HttpSession session)
    {
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        FzCarPark fzCarlot=new FzCarPark();
        fzCarlot.setcId(communityId);
        List<FzCarPark> list = fzCarParkService.selectFzCarParkList(fzCarlot);
        map.put("carpark",list);
        return prefix + "/carlot";
    }

    /**
     * 查询车位信息列表
     */
    @RequiresPermissions("system:carlot:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpSession session, FzCarlot fzCarlot)
    {
        startPage();
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        fzCarlot.setcId(communityId);
        List<FzCarlot> list = fzCarlotService.selectFzCarlotList(fzCarlot);
        return getDataTable(list);
    }

    /**
     * 导出车位信息列表
     */
    @RequiresPermissions("system:carlot:export")
    @Log(title = "车位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzCarlot fzCarlot)
    {
        List<FzCarlot> list = fzCarlotService.selectFzCarlotList(fzCarlot);
        ExcelUtil<FzCarlot> util = new ExcelUtil<FzCarlot>(FzCarlot.class);
        return util.exportExcel(list, "carlot");
    }

    /**
     * 新增车位信息
     */
    @GetMapping("/add")
    public String add(ModelMap map,HttpSession session)
    {
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        FzCarPark fzCarlot=new FzCarPark();
        fzCarlot.setcId(communityId);
        List<FzCarPark> list = fzCarParkService.selectFzCarParkList(fzCarlot);
        map.put("carparkAdd",list);
        return prefix + "/add";
    }

    /**
     * 新增保存车位信息
     */
    @RequiresPermissions("system:carlot:add")
    @Log(title = "车位信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzCarlot fzCarlot)
    {
        fzCarlot.setCarLotId(HeadNum.CAR_PARK_HEAD+DateUtils.getTimeMillis());
        fzCarlot.setcId(GetSessionValueUtil.getCommunity());
        SysUser user = ShiroUtils.getSysUser();
        fzCarlot.setCreateName(user.getUserName());
        return toAjax(fzCarlotService.insertFzCarlot(fzCarlot));
    }

    /**
     * 修改车位信息
     */
    @GetMapping("/edit/{carLotId}")
    public String edit(ModelMap map,HttpSession session,@PathVariable("carLotId") String carLotId, ModelMap mmap)
    {
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        FzCarPark fzCarlots=new FzCarPark();
        fzCarlots.setcId(communityId);
        List<FzCarPark> list = fzCarParkService.selectFzCarParkList(fzCarlots);
        map.put("carparkEdit",list);

        FzCarlot fzCarlot = fzCarlotService.selectFzCarlotById(carLotId);
        mmap.put("fzCarlot", fzCarlot);
        return prefix + "/edit";
    }

    /**
     * 修改保存车位信息
     */
    @RequiresPermissions("system:carlot:edit")
    @Log(title = "车位信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzCarlot fzCarlot)
    {   SysUser user = ShiroUtils.getSysUser();
    fzCarlot.setUpdateName(user.getUserName());
        return toAjax(fzCarlotService.updateFzCarlot(fzCarlot));
    }

    /**
     * 删除车位信息
     */
    @RequiresPermissions("system:carlot:remove")
    @Log(title = "车位信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzCarlotService.deleteFzCarlotByIds(ids));
    }
}
